﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace AudioBook.AudioManagement.FileManage
{
    public partial class Default : System.Web.UI.Page
    {
        #region Fields & constants

        private const string TYPE_OF_FOLDER = "Folder";

        #endregion Fields & constants

        #region Private methods

        private void LoadData()
        {
            LoadFile();
        }

        private void LoadFile()
        {
            string currentFolder = GlobalParam.RootFileFolder + hidCurrentFolder.Value;

            if (Directory.Exists(currentFolder))
            {
                string searchPatten = txtFilter.Text.Trim();
                if (!string.IsNullOrEmpty(searchPatten))
                {
                    if (!searchPatten.Contains("*"))
                    {
                        searchPatten = "*" + searchPatten + "*";
                    }
                }

                DirectoryInfo currentDir = new DirectoryInfo(currentFolder);

                List<DisplayFile> lstResult = new List<DisplayFile>();

                DirectoryInfo[] arrSubDir;
                FileInfo[] arrFile;

                if (!string.IsNullOrEmpty(searchPatten.Trim()))
                {
                    arrSubDir = currentDir.GetDirectories(searchPatten);
                    arrFile = currentDir.GetFiles(searchPatten);
                }
                else
                {
                    arrSubDir = currentDir.GetDirectories();
                    arrFile = currentDir.GetFiles();
                }

                foreach (DirectoryInfo itemDir in arrSubDir)
                {
                    DisplayFile enDisplayFile = new DisplayFile();
                    enDisplayFile.Name = itemDir.Name;
                    enDisplayFile.Created = itemDir.CreationTime;
                    enDisplayFile.Modified = itemDir.LastWriteTime;
                    enDisplayFile.FileType = TYPE_OF_FOLDER;
                    enDisplayFile.FileSize = 0;
                    lstResult.Add(enDisplayFile);
                }

                foreach (FileInfo itemFile in arrFile)
                {
                    DisplayFile enDisplayFile = new DisplayFile();
                    enDisplayFile.Name = itemFile.Name;
                    enDisplayFile.Created = itemFile.CreationTime;
                    enDisplayFile.Modified = itemFile.LastWriteTime;
                    enDisplayFile.FileType = itemFile.Extension;
                    enDisplayFile.FileSize = itemFile.Length;
                    lstResult.Add(enDisplayFile);
                }

                gridFile.DataSource = lstResult;
                gridFile.DataBind();
            }
        }

        private void DeleteFile()
        {
            foreach (DataGridItem item in gridFile.Items)
            {
                CheckBox chkSelected = (CheckBox)item.FindControl("chkSelected");
                if (chkSelected.Checked)
                {
                    TextBox txtCopy = (TextBox)item.FindControl("txtCopy");
                    Label lblType = (Label)item.FindControl("lblType");
                    if (lblType.Text.Equals(TYPE_OF_FOLDER))
                    {
                        try
                        {
                            Directory.Delete(GlobalParam.RootFileFolder + txtCopy.Text, true);
                        }
                        catch (Exception ex)
                        {
                            valCommon.IsValid = false;
                            valCommon.ErrorMessage = ex.Message;
                            return;
                        }
                    }
                    else
                    {
                        try
                        {
                            File.Delete(GlobalParam.RootFileFolder + txtCopy.Text);
                        }
                        catch (Exception ex)
                        {
                            valCommon.IsValid = false;
                            valCommon.ErrorMessage = ex.Message;
                            return;
                        }
                    }
                }
            }
        }

        #endregion Private methods

        #region Event handlers

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString[GUIConstants.Common.ID_QUERY] != null)
                {
                    hidCurrentFolder.Value = Request.QueryString[GUIConstants.Common.ID_QUERY];
                }
                hplAddFile.NavigateUrl = string.Format(GUIConstants.WebPage.FilePage.AddFile, hidCurrentFolder.Value);
                LoadData();
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            DeleteFile();
            LoadFile();
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            txtFilter.Text = string.Empty;
            LoadFile();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadFile();
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtFilter.Text = string.Empty;
            LoadFile();
        }

        protected void gridFile_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DisplayFile enDisplayFile = (DisplayFile)e.Item.DataItem;

                Label lblOrder = (Label)e.Item.FindControl("lblOrder");
                lblOrder.Text = (e.Item.ItemIndex + 1).ToString();

                HyperLink hplName = (HyperLink)e.Item.FindControl("hplName");
                hplName.Text = enDisplayFile.Name;

                Label lblType = (Label)e.Item.FindControl("lblType");
                lblType.Text = enDisplayFile.FileType;

                Label lblCreated = (Label)e.Item.FindControl("lblCreated");
                lblCreated.Text = enDisplayFile.Created.ToString(GUIConstants.Common.DATE_TIME_FORMAT);

                Label lblModified = (Label)e.Item.FindControl("lblModified");
                lblModified.Text = enDisplayFile.Modified.ToString(GUIConstants.Common.DATE_TIME_FORMAT);

                HyperLink hplEdit = (HyperLink)e.Item.FindControl("hplEdit");

                TextBox txtCopy = (TextBox)e.Item.FindControl("txtCopy");
                txtCopy.Text = hidCurrentFolder.Value + enDisplayFile.Name;
                txtCopy.Attributes.Add("onclick", "this.select();");

                if (!enDisplayFile.FileType.Equals(TYPE_OF_FOLDER))
                {
                    Label lblSize = (Label)e.Item.FindControl("lblSize");
                    lblSize.Text = enDisplayFile.FileSize.ToString();
                    hplEdit.NavigateUrl = string.Format(GUIConstants.WebPage.FilePage.EditFile, hidCurrentFolder.Value + enDisplayFile.Name);
                }
                else
                {
                    hplName.NavigateUrl = GUIConstants.WebPage.FilePage.DefaultFile + "?id=" + hidCurrentFolder.Value + enDisplayFile.Name + "\\";
                    hplEdit.NavigateUrl = string.Format(GUIConstants.WebPage.FilePage.EditFolder, hidCurrentFolder.Value + enDisplayFile.Name) + "\\";
                }
            }
        }

        #endregion Event handlers
    }

    public class DisplayFile
    {
        public string Name { get; set; }
        public DateTime Created { get; set; }
        public DateTime Modified { get; set; }
        public string FileType { get; set; }
        public long FileSize { get; set; }
    }
}
